<template>
  <el-container class="layout-container">
    <!-- 顶部标题栏 -->
    <el-header class="top-header">
      <div class="header-content">
        <div class="logo">
          <el-icon><Setting /></el-icon>
          <span>餐厅管理系统</span>
        </div>
        <div class="header-info">
          <el-tag type="info" size="large">管理员</el-tag>
        </div>
      </div>
    </el-header>

    <el-container class="main-container">
      <!-- 左侧功能菜单 -->
      <el-aside class="sidebar" :width="isCollapsed ? '64px' : '240px'">
        <div class="sidebar-header">
          <el-button 
            type="text" 
            @click="toggleCollapse"
            class="collapse-btn"
            :icon="isCollapsed ? Expand : Fold"
          >
            {{ isCollapsed ? '' : '收起菜单' }}
          </el-button>
        </div>
        
        <el-menu
          :default-active="activeMenu"
          :collapse="isCollapsed"
          :unique-opened="true"
          @select="handleMenuSelect"
          class="sidebar-menu"
        >
          <el-menu-item index="/admin/dishes">
            <el-icon><Dish /></el-icon>
            <template #title>菜品信息</template>
          </el-menu-item>
          <el-menu-item index="/admin/orders">
            <el-icon><List /></el-icon>
            <template #title>订单信息</template>
          </el-menu-item>
          <el-menu-item index="/admin/comments">
            <el-icon><ChatLineSquare /></el-icon>
            <template #title>订单评价</template>
          </el-menu-item>
          <el-menu-item index="/admin/users">
            <el-icon><User /></el-icon>
            <template #title>用户管理</template>
          </el-menu-item>
        </el-menu>
        
        <!-- 用户信息区域 -->
        <div class="user-section">
          <el-dropdown @command="handleUserCommand" placement="top-start">
            <div class="user-info">
              <el-avatar :size="40" :src="userStore.user?.avatar">
                {{ userStore.user?.nickname?.charAt(0) || 'A' }}
              </el-avatar>
              <div class="user-details" v-if="!isCollapsed">
                <div class="username">{{ userStore.user?.nickname || '管理员' }}</div>
                <div class="user-role">系统管理员</div>
              </div>
              <el-icon class="dropdown-icon" v-if="!isCollapsed"><ArrowDown /></el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-aside>

      <!-- 主内容区域 -->
      <el-main class="main-content">
        <div class="content-wrapper">
          <router-view />
        </div>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { useUserStore } from '@/stores/user';
import { 
  Setting, 
  ArrowDown, 
  Dish, 
  List, 
  ChatLineSquare, 
  User, 
  Expand, 
  Fold 
} from '@element-plus/icons-vue';
import { ElMessageBox } from 'element-plus';

const router = useRouter();
const route = useRoute();
const userStore = useUserStore();

const isCollapsed = ref(false);

const activeMenu = computed(() => route.path);

const handleMenuSelect = (key: string) => {
  router.push(key);
};

const toggleCollapse = () => {
  isCollapsed.value = !isCollapsed.value;
};

const handleUserCommand = (command: string) => {
  if (command === 'logout') {
    ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      userStore.logout();
      router.push('/login');
    }).catch(() => {});
  } else if (command === 'profile') {
    router.push('/admin/profile');
  }
};
</script>

<style scoped>
/* 使用CSS变量覆盖Element Plus默认样式 */
:deep(.el-menu) {
  --el-menu-bg-color: transparent;
  --el-menu-text-color: #CFD8DC;
  --el-menu-hover-bg-color: rgba(0, 188, 212, 0.3);
  --el-menu-hover-text-color: #fff;
  --el-menu-active-bg-color: #00BCD4;
  --el-menu-active-text-color: #fff;
}

/* 强制子菜单样式 */
:deep(.el-sub-menu .el-menu-item) {
  background: transparent !important;
  background-color: transparent !important;
  color: #CFD8DC !important;
  margin: 4px 12px 4px 24px !important;
  border-radius: 12px !important;
  font-size: 14px !important;
  padding-left: 20px !important;
  border-left: 3px solid transparent !important;
  transition: all 0.3s ease !important;
}

:deep(.el-sub-menu .el-menu-item:hover) {
  background: linear-gradient(135deg, rgba(0, 188, 212, 0.3) 0%, rgba(0, 172, 193, 0.2) 100%) !important;
  background-color: rgba(0, 188, 212, 0.3) !important;
  color: #fff !important;
  transform: translateX(6px) !important;
  box-shadow: 0 4px 12px rgba(0, 188, 212, 0.2) !important;
}

:deep(.el-sub-menu .el-menu-item.is-active) {
  background: linear-gradient(135deg, #00BCD4 0%, #00ACC1 100%) !important;
  background-color: #00BCD4 !important;
  color: #fff !important;
  box-shadow: 0 4px 12px rgba(0, 188, 212, 0.4) !important;
  font-weight: 600 !important;
}

/* 最强制性的覆盖 */
:deep(.el-menu--vertical .el-sub-menu .el-menu-item),
:deep(.el-menu--vertical .el-sub-menu .el-menu-item.el-menu-item),
:deep(.el-menu--vertical .el-sub-menu .el-menu-item.el-menu-item.el-menu-item) {
  background: transparent !important;
  background-color: transparent !important;
  color: #CFD8DC !important;
}

:deep(.el-menu--vertical .el-sub-menu .el-menu-item:hover),
:deep(.el-menu--vertical .el-sub-menu .el-menu-item.el-menu-item:hover),
:deep(.el-menu--vertical .el-sub-menu .el-menu-item.el-menu-item.el-menu-item:hover) {
  background: linear-gradient(135deg, rgba(0, 188, 212, 0.3) 0%, rgba(0, 172, 193, 0.2) 100%) !important;
  background-color: rgba(0, 188, 212, 0.3) !important;
  color: #fff !important;
}

:deep(.el-menu--vertical .el-sub-menu .el-menu-item.is-active),
:deep(.el-menu--vertical .el-sub-menu .el-menu-item.el-menu-item.is-active),
:deep(.el-menu--vertical .el-sub-menu .el-menu-item.el-menu-item.el-menu-item.is-active) {
  background: linear-gradient(135deg, #00BCD4 0%, #00ACC1 100%) !important;
  background-color: #00BCD4 !important;
  color: #fff !important;
}
    .layout-container {
      min-height: 100vh;
      background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
      animation: bgFade 0.8s ease-out;
    }

@keyframes bgFade {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

    /* 顶部标题栏 */
    .top-header {
      background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      box-shadow: 0 8px 32px rgba(102, 126, 234, 0.3);
      padding: 0;
      height: 70px;
      line-height: 70px;
      animation: slideDown 0.5s ease-out;
      position: relative;
      z-index: 1000;
      backdrop-filter: blur(10px);
    }

@keyframes slideDown {
  from {
    transform: translateY(-100%);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes pulse {
  0%, 100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
}

@keyframes shimmer {
  0% {
    background-position: -200% 0;
  }
  100% {
    background-position: 200% 0;
  }
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  padding: 0 24px;
}

.logo {
  display: flex;
  align-items: center;
  font-size: 22px;
  font-weight: 700;
  color: #fff;
  transition: all 0.3s ease;
  cursor: pointer;
  animation: pulse 3s infinite;
}

.logo:hover {
  transform: scale(1.05);
  text-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
}

.logo .el-icon {
  font-size: 28px;
  margin-right: 12px;
  animation: shimmer 2s infinite;
  background: linear-gradient(90deg, #ffffff 0%, #f0f0f0 50%, #ffffff 100%);
  background-size: 200% 100%;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.1);
  }
}

.user-dropdown {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #fff;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.user-dropdown:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.username {
  font-weight: 500;
  font-size: 14px;
}

/* 主容器 */
.main-container {
  height: calc(100vh - 60px);
}

    /* 左侧边栏 */
    .sidebar {
      background: linear-gradient(180deg, #2d3748 0%, #4a5568 100%);
      box-shadow: 8px 0 32px rgba(45, 55, 72, 0.4);
      transition: all 0.3s ease;
      overflow: hidden;
      position: relative;
      display: flex;
      flex-direction: column;
      backdrop-filter: blur(10px);
    }

.sidebar-header {
  padding: 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.collapse-btn {
  width: 100%;
  color: #fff;
  font-size: 12px;
  transition: all 0.3s ease;
}

.collapse-btn:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.sidebar-menu {
  border: none;
  background: transparent;
  padding: 8px 0;
  flex: 1;
  overflow-y: auto;
}

.sidebar-menu :deep(.el-menu-item) {
  color: #CFD8DC;
  margin: 4px 12px;
  border-radius: 12px;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
  font-weight: 500;
}

.sidebar-menu :deep(.el-menu-item:hover) {
  background: linear-gradient(135deg, rgba(0, 188, 212, 0.3) 0%, rgba(0, 172, 193, 0.2) 100%);
  color: #fff;
  transform: translateX(6px);
  box-shadow: 0 4px 12px rgba(0, 188, 212, 0.2);
}

.sidebar-menu :deep(.el-menu-item.is-active) {
  background: linear-gradient(135deg, #00BCD4 0%, #00ACC1 100%);
  color: #fff;
  box-shadow: 0 4px 12px rgba(0, 188, 212, 0.4);
  font-weight: 600;
}

.sidebar-menu :deep(.el-menu-item.is-active::before) {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 4px;
  background: #fff;
  border-radius: 0 2px 2px 0;
}

.sidebar-menu :deep(.el-menu-item .el-icon) {
  font-size: 18px;
  margin-right: 8px;
}


/* 用户信息区域 */
.user-section {
  margin-top: auto;
  padding: 16px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.1) 0%, rgba(255, 255, 255, 0.05) 100%);
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
  color: #fff;
  cursor: pointer;
  padding: 8px;
  border-radius: 12px;
  transition: all 0.3s ease;
}

.user-info:hover {
  background: rgba(255, 255, 255, 0.15);
  transform: translateY(-1px);
}

.user-details {
  flex: 1;
  min-width: 0;
}

.username {
  font-weight: 600;
  font-size: 14px;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-role {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.7);
  margin-top: 2px;
}

.dropdown-icon {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.7);
  transition: transform 0.3s ease;
}

.user-info:hover .dropdown-icon {
  transform: rotate(180deg);
}

    /* 主内容区域 */
    .main-content {
      background: linear-gradient(135deg, #ffffff 0%, #f8fafc 100%);
      padding: 0;
      overflow-y: auto;
      position: relative;
    }
    
    .main-content::before {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: radial-gradient(circle at 20% 50%, rgba(102, 126, 234, 0.05) 0%, transparent 50%),
                  radial-gradient(circle at 80% 20%, rgba(118, 75, 162, 0.05) 0%, transparent 50%);
      pointer-events: none;
    }

.content-wrapper {
  padding: 24px;
  min-height: 100%;
  animation: fadeInUp 0.6s ease-out;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 60px;
    height: calc(100vh - 60px);
    z-index: 999;
    transform: translateX(-100%);
  }
  
  .sidebar.show {
    transform: translateX(0);
  }
  
  .main-content {
    margin-left: 0;
  }
}
</style>

